package com.duojuhe.common.utils.httputils;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * http请求
 *
 * @date 2018-05-30
 */
@Slf4j
public class HttpUtils {
    private static OkHttpClient tlClient;

    static {
        ConnectionPool tlPool = new ConnectionPool(50, 5, TimeUnit.MINUTES);
        tlClient = new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .connectionPool(tlPool)
                .followRedirects(false)
                .retryOnConnectionFailure(false)
                .build();
    }



    /**
     * 同步方式去获取网络请求返回
     *
     * @param url
     * @param content
     * @return
     */
    public static String syncPost(String url, String content,String appKey,String sign) {
        try {
            RequestBody body = RequestBody.create(MediaType.parse("application/json"), content);
            Request request = new Request.Builder()
                    .method("POST", body)
                    .url(url)
                    .header("appKey", appKey)
                    .header("sign", sign)
                    .header("Connection", "close")
                    .header("catch-control", "no-catch")
                    .build();
            Response response = tlClient.newCall(request).execute();
            if (response.isSuccessful()) {
                ResponseBody responseBody = response.body();
                if (responseBody != null) {
                    return responseBody.string();
                }
            }
            log.error("collect response error, code={}", response.code());
        } catch (Exception e) {
            log.error("collect request fail, url={}", url, e);
        }
        return null;
    }

    /**
     * 同步方式去获取网络请求返回
     *
     * @param url            请求地址
     * @param connectTimeout 连接超时时间
     * @param readTimeout    Response读取超时时间
     */
    public static String syncGet(String url, int connectTimeout, int readTimeout) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = tlClient.newCall(request).execute();
            if (response.isSuccessful()) {
                ResponseBody responseBody = response.body();
                if (responseBody != null) {
                    return responseBody.string();
                }
            }
            log.error("collect response error, code={}", response.code());
        } catch (Exception e) {
            log.error("collect request fail, url={}, connectTimeout={}, readTimeout={}", url,
                    connectTimeout, readTimeout, e);
        }
        return null;
    }

    /**
     * 判断客户端浏览器类型
     *
     * @param request
     * @return
     */
    public static String getBrowser(HttpServletRequest request) {
        String UserAgent = request.getHeader("User-Agent").toLowerCase();
        if (UserAgent.contains("firefox")) {
            return "FF";
        } else if (UserAgent.contains("safari")) {
            return "Chrome";
        } else {
            return "IE";
        }
    }


    public static void main(String[] args) {
    }
}
